$(document).ready(function() {
	$("#add_stay").live('click', function() {
		$.ajax({
			type : "GET",
			url : "stay",
			data : "action=dialogStay",
			success : function(response) {
				$("#dialog_stay").html(response);
				$("#dialog_stay").dialog('open');
			}
		});
	});
	$("#delete_stay").live('click',function(){
		var stayId = $(this).attr('stayid');
		$.ajax({
			type : "GET",
			url : "stay",
			data : "action=deleteStay&stayid="+stayId,
			success : function(response) {
				if (response == 1){
					$(".stay-"+stayId).remove();
				}
			}
		});
	});
	$("#edit_stay").live('click', function() {
		var stayId = $(this).attr('stayid');
		$.ajax({
			type : "GET",
			url : "stay",
			data : "action=dialogStay&stayid="+stayId,
			success : function(response) {
				$("#dialog_stay").html(response);
				$("#dialog_stay").dialog('open');
			}
		});
	});
	$("#btn_save_stay").live('click',function(){
		var stayid = $("[name=dialog_stay_id]").val();
		var employee = $("[name=dialog_stay_employee]").val();
		var stayType = $("[name=dialog_stay_type]").val();
		var trainingType = $("[name=dialog_stay_training_type]").val();
		var start_date_date = $("[name=dialog_stay_start_date]").val();
		var start_date_time = $("[name=dialog_stay_start_time]").val();
		var start_date = start_date_date.trim() + " " + start_date_time.trim();
		var end_date_date = $("[name=dialog_stay_end_date]").val();
		var end_date_time = $("[name=dialog_stay_end_time]").val();
		var end_date = end_date_date.trim() + " " + end_date_time.trim();
		var location = $("[name=dialog_stay_location]").val();
		
		var data = 	"&stayid=" + stayid +
					"&employee=" + employee + 
					"&staytype=" + stayType + 
					((stayType=="TRAINING")?("&trainingtype=" + trainingType):"") + 
					"&start_date=" + start_date + 
					"&end_date=" + end_date + 
					"&location=" + location;
		
		$.ajax({
			type : "GET",
			url : "stay",
			data : "action=saveStay" + data,
			success : function(response) {
				if (response != 0){
					if (stayid == -1){
						$("#stay_list_container").html(response);
					}else{
						$('tr[stay_row="' + stayid + '"]').html(response);
					}
					$("#dialog_stay").dialog('close');
				}
			}
		});
	});
	$("#btn_cancel_stay").live('click',function(){
		$("#dialog_stay").dialog('close');
	});
});